import { createRouter, createWebHashHistory,createWebHistory , RouteRecordRaw } from 'vue-router'
import Login from '../views/Login.vue'
import MainBox from '../views/MainBox.vue'
import ShortUrl from '../views/ShortUrl.vue'
import routesConfig from './config'
import NotFound from '../views/notfound/NotFound.vue'

const routes: Array<RouteRecordRaw> = [
    {
      path:"/login",
      name:"login",
      component:Login
    },
    
    {
      path:"/:pathMatch(.*)",
      component:ShortUrl
    },

    {
      path:"/",
      redirect:'/index'
    },

    {
      path:"/",
      name:"mainbox",
      component:MainBox
    },
    {
      path:"/notfound",
      component:NotFound
    }
]

const router = createRouter({
  history: createWebHistory (),
  routes
})
//添加路由
routesConfig.forEach(item=>{
  router.addRoute("mainbox",item)
})

//路由拦截
router.beforeEach((to,form,next) => {
    console.log("🚀 ~ router.beforeEach ~ to:", to)
    // start()
   if(to.fullPath==='/login' || to.params.pathMatch){
    	next()
  	}else{
    	if(!localStorage.getItem("token")){
      	next({path:"/login"})
    	}
    	next()
  	}
	
})
// router.afterEach((to,form,next) => {
//   close()
// })
export default router
